package com.jinboe.comm.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.beanutils.Converter;

/**
 * 重写日期转换
 * 
 * @author jinbo
 */
public class DateConvert implements Converter {
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	public Object convert(Class oClass, Object originalObject) {
		
		if(originalObject==null&&oClass.toString().equalsIgnoreCase("class java.lang.String")){
			return convertString(oClass, originalObject);
		}else if(originalObject instanceof String){
			return convertString(oClass, originalObject);
		}else if (originalObject instanceof Date) {
			return convertDate(oClass, originalObject);
		}
		return null;
	}
	public String convertDate(Class oClass, Object originalObject) {
		
		try {
			String formDateStr= dateFormat.format(originalObject);
			return formDateStr;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public Object convertString(Class oClass, Object originalObject) {
		
		if(oClass.toString().equalsIgnoreCase("class java.util.Date")){
			return DateUtil.stringToDate((String)originalObject);
		}
		if(originalObject==null){
			return "";
		}
		return originalObject;
	}
}